package emas.server.authentication;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import emas.server.core.MySQLConnectionManager;
import emas.server.encrypt.BCrypt;

public class CreateUserServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String username = req.getParameter("username");
		String password = req.getParameter("password");

		if (username != null && password != null) {
			MySQLConnectionManager manager = MySQLConnectionManager
					.getInstance();

			String hashedpass = BCrypt.hashpw(password, BCrypt.gensalt());

			manager.query("INSERT INTO user(user_id,username,hashpassword) VALUES(0,"
					+ "'" + username + "'" + "," + "'" + hashedpass + "'" + ")");
		}
		super.doGet(req, resp);
	}

}
